package r6.powtorka;

import java.text.DateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class Employee implements Cloneable {

	private static int nextID;

	static {
		nextID = 1;
	}

	private final int id = getNextID();
	private final String name;
	private Date hireDate;
	private double salary;

	public Employee(String name, int year, int month, int day, double salary) {
		this.name = name;
		this.hireDate = new GregorianCalendar(year, month - 1, day).getTime();
		this.salary = salary;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Employee))
			return false;
		Employee other = (Employee) obj;
		if (hireDate == null) {
			if (other.hireDate != null)
				return false;
		} else if (!hireDate.equals(other.hireDate))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(salary) != Double
				.doubleToLongBits(other.salary))
			return false;
		return true;
	}

	public Date getHireDate() {
		return (Date) hireDate.clone();
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public double getSalary() {
		return salary;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((hireDate == null) ? 0 : hireDate.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(salary);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	public void increasesalary(double byPercent) {
		this.salary += this.salary * byPercent / 100;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	@Override
	public String toString() {
		return this.getClass().getSimpleName() + " [" + this.id + "] "
				+ this.name + ", zatrudniony "
				+ DateFormat.getDateInstance().format(this.hireDate)
				+ ", zarabia: " + this.salary;
	}

	private int getNextID() {
		return Employee.nextID++;
	}

	@Override
	protected Employee clone() throws CloneNotSupportedException {
		Employee cloned = (Employee) super.clone();
		cloned.hireDate = (Date) this.hireDate.clone();
		return cloned;
	}
}
